package com.scalar.chiptrack.adaptor.sitime.utac;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.CSVParserImpl;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.ParameterTypes;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 21 Dec, 2009
 * Time: 10:56:33 PM
 */
public class UTACDataParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;

    private HashMap<String, AssemblyLot> m_assyWipDataMap   = new HashMap<String, AssemblyLot>();
    private HashMap<String, TestLot> m_testDataMap          = new HashMap<String, TestLot>();
    private HashMap<String, InventoryLot> m_dieBankDataMap  = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_backGrindDataMap  = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_preTestDataMap  = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_fgDataMap       = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_trDataMap	    = new HashMap<String, InventoryLot>();

    public UTACDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_UTAC, WIP_TYPE_ASSEMBLY );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_UTAC );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );   // All the DAOs and Parser should be logging into same file
            TestAdaptorDAO.setLogger( m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading/creating UTAC output directory from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new UTACDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    //Execute Method
   public void execute() throws Exception
   {
       AdaptorUtils.printHeader(VENDOR_NAME_UTAC, CONSTANT_PARSER );

       File doneDestDir = new File( m_dataOutputDir + "/done" );

       File wipDataoutDir = new File( m_dataOutputDir );

       File[] fileList = wipDataoutDir.listFiles();

       if ( ( fileList == null ) || ( fileList.length <= 3 ) )
       {
           log( "No file found in directory: " + wipDataoutDir );
           throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
       }

       ArrayList<File> successFileList   = new ArrayList<File>();
       boolean success             = false;

       try
       {
           for (File aFileList : fileList)
           {
               if (aFileList.isDirectory()) {
                   continue;
               }

               if ((aFileList.getName() != null) && (aFileList.getName().trim().indexOf(UTACPOP3Adaptor.FILE_NAME_ASSY_WIP) != -1))
               {
                   parseDailyWipReport(aFileList.getAbsolutePath());

                   successFileList.add(aFileList);
               }
               if ((aFileList.getName() != null) && (aFileList.getName().trim().indexOf(UTACPOP3Adaptor.FILE_NAME_TEST_WIP) != -1))
               {
                   parseDailyTestWipReport(aFileList.getAbsolutePath());

                   successFileList.add(aFileList);
               }
           }
       }
       catch ( Exception e )
       {
           success = false;
           log( "ERROR!! ERROR!! ERROR!! While parsing and storing File " + e );
           e.printStackTrace();
           throw e;
       }

        try
        {
            // Save Wafer Store Information
            if (m_backGrindDataMap.isEmpty() == false)
            {
                log(" Updating Back Grind Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_backGrindDataMap.values().iterator(), LOCATION_TYPE_BACK_GRIND, VENDOR_NAME_UTAC);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_BACK_GRIND, VENDOR_NAME_UTAC);
            }
            success = true;
            m_backGrindDataMap.clear();
            m_backGrindDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
            throw se;
        }
       // to save the Die Bank lot tables
       try
       {
           if ( m_dieBankDataMap.isEmpty() == false )
           {
               log(" Updating Die Bank Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }

       // to save the Assembly lot tables
       try
       {
           if ( m_assyWipDataMap.isEmpty() == false )
           {
               log(" Updating Assembly Lot Information");
               AssemblyAdaptorDAO.saveAssemblyLot( null, m_assyWipDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               AssemblyAdaptorDAO.closeAssemblyLot(null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }
        // to save the Class Stroe lot tables
       try
       {
           if ( m_preTestDataMap.isEmpty() == false )
           {
               log(" Updating Pre Test Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_preTestDataMap.values().iterator(), LOCATION_TYPE_PRE_TEST, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_PRE_TEST, VENDOR_NAME_UTAC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Pre Test Lot information into database: " + se);
          throw se;
      }

      /*Updating the Test Lot Information*/
      try
      {
          // Save Test Information
          if( m_testDataMap.isEmpty() == false )
          {
              log(" Updating Test Lot Information");
              TestAdaptorDAO.saveTestLot( null, m_testDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_UTAC );
              success = true;
          }
          else
          {
              TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_UTAC );
          }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
          throw se;
      }

      // to save the Tape & Reel lot info
      try
      {
          if ( m_trDataMap.isEmpty() == false )
          {
              log(" Updating Tape & Reel Lot Information");
              InventoryLotDAO.saveInventoryLot( null, m_trDataMap.values().iterator(), LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_UTAC );
              success = true;
          }
          else
          {
              InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_TAPE_REEL, VENDOR_NAME_UTAC );
          }
      }
      catch (ScalarException se)
      {
         success = false;
         se.printStackTrace();
         log("ERROR!! ERROR!! ERROR!! Could not update Tape & Reel Lot information into database: " + se);
         throw se;
      }

       // to save the FG lot tables
       try
       {
           if ( m_fgDataMap.isEmpty() == false )
           {
               log(" Updating Finshed Goods Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_FG, VENDOR_NAME_UTAC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Finshed Goods Lot information into database: " + se);
          throw se;
      }
      // To Close the Drop Out Lots
      AdaptorDAO.closeDropOutLots( VENDOR_NAME_UTAC );

      // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
      AdaptorDAO.sendAllNotifications( VENDOR_NAME_UTAC, true, true, true );

      int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

      // Update Adaptor_Run_Info table
      try
      {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
      }
      catch ( ScalarException se )
      {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
       }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
   }//end of execute method


    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private void parseDailyWipReport(String fileName) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result            = null;

        InventoryLot inventoryLot  = null;
        AssemblyLot  assemblyLot   = null;
        String partNumber       = null;
        String fabLotNumber     = null;
        String lotNumber        = null;
        
        try
        {
            File detailWipFile = new File( fileName );
            if ( !detailWipFile.exists() )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            Parser parser = ParserFactory.getCSVParser( detailWipFile , ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            log( "\nFound " + fileName );
            log( "\nParsing the file for ASSY Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for ASSY Wip Report..." );

            result = parser.parse();

            while ( result.next() )
            {
                partNumber      = result.getString("Product No");
                fabLotNumber    = result.getString("Wafer Lot No");
                lotNumber = result.getString("Cust Assy Lot");
                if ( lotNumber == null )
                {
                    lotNumber = fabLotNumber;
                }
                String allWafers = null;
                if ( partNumber != null && partNumber.trim().length() > 0 && partNumber.indexOf("Total") == -1 && !partNumber.startsWith("---")  
                        && lotNumber != null && lotNumber.trim().length() > 0 )
                {
                    if ( fabLotNumber != null && "N/A".equalsIgnoreCase(fabLotNumber) )
                    {
                        fabLotNumber = lotNumber;
                    }
                    if ( fabLotNumber == null )
                    {
                        fabLotNumber = lotNumber;
                    }
                    if ( fabLotNumber != null && fabLotNumber.indexOf("/") != -1 )
                    {
                        fabLotNumber = fabLotNumber.substring(0, fabLotNumber.indexOf("/") );
                    }

                    String waferIds = null;
                    if ( result.getString("Wafer Number") != null && result.getString("Wafer Number").trim().length() > 0 )
                    {
                        waferIds = result.getString("Wafer Number").replaceAll("[a-zA-Z]", "").replaceAll(",", ";");
                    }

                    if ( result.getString("Package Code") != null && result.getString("Package Code").startsWith("WGRD")
                            && result.getInt("GRINDING") > 0 )
                    {
                        inventoryLot = (InventoryLot)m_backGrindDataMap.get( partNumber + fabLotNumber );

                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_UTAC, LOCATION_TYPE_BACK_GRIND );
                            m_backGrindDataMap.put( partNumber + fabLotNumber, inventoryLot );
                        }

                        int wquantity = 0;
                        if (inventoryLot.getWaferQtyIn() != null)
                        {
                            try
                            {
                                wquantity = Integer.parseInt(inventoryLot.getWaferQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            int fabProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, WIP_TYPE_FAB, WIP_TYPE_FAB );
                            int gdpw = 0;
                            try
                            {
                                gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                            }
                            catch( NumberFormatException nfe ) { gdpw = 0; }

                             wquantity += (result.getInt("GRINDING") )/ gdpw;
                        }
                        else
                        {
                            int fabProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, WIP_TYPE_FAB, WIP_TYPE_FAB );
                            int gdpw = 0;
                            try
                            {
                                gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                            }
                            catch( NumberFormatException nfe ) { gdpw = 0; }

                            wquantity = (result.getInt("GRINDING") )/gdpw;
                        }
                        if ( inventoryLot.getWaferId() != null )
                        {
                            allWafers = inventoryLot.getWaferId() +";" + waferIds;
                        }
                        else
                        {
                            allWafers = waferIds;
                        }
                        if (wquantity > 0 )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( fabLotNumber );
                            inventoryLot.setWaferQtyIn(wquantity + "");
                            inventoryLot.setDateIn( result.getString("Wafer Receive Date") );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setWaferId( allWafers );
                        }
                    }//END OF BACK GRIND

                    if ( result.getInt("DieBank") > 0 )
                    {
                        log("In Die Bank " + lotNumber );

                        inventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + fabLotNumber );
                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_UTAC, LOCATION_TYPE_DIE_BANK );
                            m_dieBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                        }
                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += result.getInt("DieBank");
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            quantity = result.getInt("DieBank");
                        }
                        if ( inventoryLot.getWaferId() != null )
                        {
                            allWafers = inventoryLot.getWaferId() +";" + waferIds;
                        }
                        else
                        {
                            allWafers = waferIds;
                        }
                        if ( quantity > 0 )
                        {
                            inventoryLot.setLotNumber( fabLotNumber );
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setDateIn( result.getString("Wafer Receive Date") );
                            inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setQtyIn( quantity +"" );
                            inventoryLot.setWaferId( allWafers );
                        }
                    }
                    //ASSEMBLY
                    if( result.getString("Package Code") != null && !result.getString("Package Code").startsWith("WGRD")
                         && ( result.getInt("XFER-GRIND") > 0 || result.getInt("GRINDING") > 0 || result.getInt("XFER-ASSY") > 0 || result.getInt("2/OPT STAT") > 0
                        || result.getInt("SAW") > 0 || result.getInt("DIE ATTACH") > 0 || result.getInt("POLY CURE") > 0 || result.getInt("3/OPT") > 0
                        || result.getInt("LEAD BOND") > 0 || result.getInt("SEAL") > 0 || result.getInt("PKG CURE") > 0 || result.getInt("TOP MARK") > 0
                        || result.getInt("DETAPE") > 0 || result.getInt("SINGULATE") > 0  || result.getInt("PLATE") > 0
                        || result.getInt("AFOI") > 0 || result.getInt("APACK") > 0 || result.getInt("FGS") > 0 || result.getInt("Others") > 0) )
                    {
                        log( "In Assembly " + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC );
                            m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                        }

                        // FOR SETTING THE QTY IN
                        if( result.getInt("XFER-GRIND") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("XFER-GRIND") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("XFER-GRIND")+ "");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("XFER-GRIND") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"XFER-GRIND".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", XFER-GRIND") ;
                            }
                            else
                            {
                                assemblyLot.setStage("XFER-GRIND");
                            }
                        }
                        if( result.getInt("GRINDING") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("GRINDING") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("GRINDING") +"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("GRINDING") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"GRINDING".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", GRINDING") ;
                            }
                            else
                            {
                                assemblyLot.setStage("GRINDING");
                            }
                        }
                        if( result.getInt("XFER-ASSY") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("XFER-ASSY") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("XFER-ASSY") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("XFER-ASSY")+"" );
                            }
                            if ( assemblyLot.getStage() != null && !"XFER-ASSY".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", XFER-ASSY") ;
                            }
                            else
                            {
                                assemblyLot.setStage("XFER-ASSY");
                            }
                        }
                        if( result.getInt("2/OPT STAT") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("2/OPT STAT") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("2/OPT STAT") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("2/OPT STAT") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"2/OPT STAT".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", 2/OPT STAT") ;
                            }
                            else
                            {
                                assemblyLot.setStage("2/OPT STAT");
                            }
                        }
                        if( result.getInt("SAW") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("SAW") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("SAW") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("SAW") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"SAW".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", SAW") ;
                            }
                            else
                            {
                                assemblyLot.setStage("SAW");
                            }
                        }

                        if( result.getInt("DIE ATTACH") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("DIE ATTACH") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("DIE ATTACH") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("DIE ATTACH") + "");
                            }
                            if ( assemblyLot.getStage() != null && !"DIE ATTACH".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", DIE ATTACH") ;
                            }
                            else
                            {
                                assemblyLot.setStage("DIE ATTACH");
                            }

                        }
                        if( result.getInt("POLY CURE") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("POLY CURE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("POLY CURE") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("POLY CURE") + "" );
                            }
                            if ( assemblyLot.getStage() != null && !"POLY CURE".equalsIgnoreCase( assemblyLot.getStage() ))
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", POLY CURE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("POLY CURE");
                            }
                        }
                        if( result.getInt("3/OPT") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("3/OPT") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("3/OPT") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("3/OPT") + "");
                            }
                            if ( assemblyLot.getStage() != null && !"3/OPT".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", 3/OPT") ;
                            }
                            else
                            {
                                assemblyLot.setStage("3/OPT");
                            }
                        }
                        if( result.getInt("LEAD BOND") > 0  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("LEAD BOND") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("LEAD BOND") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("LEAD BOND") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"LEAD BOND".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", LEAD BOND") ;
                            }
                            else
                            {
                                assemblyLot.setStage("LEAD BOND");
                            }

                        }
                        if( result.getInt("SEAL") > 0  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("SEAL")) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("SEAL") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("SEAL") + "" );
                            }
                            if ( assemblyLot.getStage() != null && !"SEAL".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", SEAL") ;
                            }
                            else
                            {
                                assemblyLot.setStage("SEAL");
                            }

                        }
                        if( result.getInt("PKG CURE") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("PKG CURE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("PKG CURE") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("PKG CURE") + "");
                            }
                            if ( assemblyLot.getStage() != null && !"PKG CURE".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", PKG CURE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("PKG CURE");
                            }
                        }
                        if( result.getInt("TOP MARK") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("TOP MARK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("TOP MARK") +"" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("TOP MARK") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"TOP MARK".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", TOP MARK") ;
                            }
                            else
                            {
                                assemblyLot.setStage("TOP MARK");
                            }

                        }
                        if( result.getInt("DETAPE") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("DETAPE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("DETAPE") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("DETAPE") +"" );
                            }
                            if ( assemblyLot.getStage() != null && !"DETAPE".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", DETAPE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("DETAPE");
                            }
                        }
                        if( result.getInt("SINGULATE") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("SINGULATE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("SINGULATE") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("SINGULATE") + "" );
                            }
                            if ( assemblyLot.getStage() != null && !"SINGULATE".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", SINGULATE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("SINGULATE");
                            }
                        }
                                                
                        if( result.getInt("PLATE") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("PLATE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("PLATE") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("PLATE") +"");
                            }
                            if ( assemblyLot.getStage() != null && !"PLATE".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", PLATE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("PLATE");
                            }
                        }
                        
                        if( result.getInt("AFOI") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("AFOI") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("AFOI") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("AFOI") +"");
                            }
                            if ( assemblyLot.getStage() != null && !"AFOI".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", AFOI") ;
                            }
                            else
                            {
                                assemblyLot.setStage("AFOI");
                            }
                        }
                        if( result.getInt("APACK") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("APACK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("APACK") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("APACK") +"");
                            }
                            if ( assemblyLot.getStage() != null && !"APACK".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", APACK") ;
                            }
                            else
                            {
                                assemblyLot.setStage("APACK");
                            }
                        }
                        if( result.getInt("FGS") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("FGS") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("FGS") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("FGS") +"");
                            }
                            if ( assemblyLot.getStage() != null && !"FGS".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", FGS") ;
                            }
                            else
                            {
                                assemblyLot.setStage("FGS");
                            }
                        }
                        if( result.getInt("Others") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + result.getInt("Others") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( result.getInt("Others") + "" );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( result.getInt("Others") +"");
                            }
                            if ( assemblyLot.getStage() != null && !"Others".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Others") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Others");
                            }
                        }

                        if ( assemblyLot.getQtyIn() != null && assemblyLot.getQtyIn().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus( LOT_STATUS_RUNNING );
                            assemblyLot.setFabLotNumber( fabLotNumber );
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if (result.getString("Start Date") != null )
                            {
                                assemblyLot.setDateIn( result.getString("Start Date")  );
                            }

                            if (result.getString("Plan Comp Date") != null )
                            {
                                assemblyLot.setExpectedDateOut( result.getString("Plan Comp Date") );
                            }
                            assemblyLot.setDateCode( result.getString("Date Code"));
                            assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );
                        }
                    }
                }
            }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            System.out.println("!!ERROR !!ERROR Exception while parsting the UTAC Assy WIP sheet... = " + e);
            throw e;
        }
    }
    private void parseDailyTestWipReport(String fileName) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result            = null;

        InventoryLot inventoryLot  = null;
        TestLot      testLot       = null;
        String partNumber       = null;
        String fabLotNumber     = null;
        String lotNumber        = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( !detailWipFile.exists() )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            Parser parser = ParserFactory.getCSVParser( detailWipFile , ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            log( "\nFound " + fileName );
            log( "\nParsing the file for Test Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Test Wip Report..." );

            result = parser.parse();

            while ( result.next() )
            {
                partNumber      = result.getString("Product No");
                fabLotNumber    = result.getString("Wafer Lot No");
                lotNumber = result.getString("Cust Assy Lot");
                if ( lotNumber == null )
                {
                    lotNumber = fabLotNumber;
                }
                if ( partNumber != null && partNumber.trim().length() > 0 && partNumber.indexOf("Total") == -1 && !partNumber.startsWith("---")
                        && lotNumber != null && lotNumber.trim().length() > 0 )
                {
                    if ( fabLotNumber != null && "N/A".equalsIgnoreCase(fabLotNumber) )
                    {
                        fabLotNumber = lotNumber;
                    }
                    if ( fabLotNumber == null )
                    {
                        fabLotNumber = lotNumber;
                    }
                    if ( fabLotNumber != null && fabLotNumber.indexOf("/") != -1 )
                    {
                        fabLotNumber = fabLotNumber.substring(0, fabLotNumber.indexOf("/") );
                    }

                    if ( result.getInt("Pre test inv") > 0  )
                    {
                        log("IN Pre Test = " + lotNumber);
                        inventoryLot = m_preTestDataMap.get( lotNumber + fabLotNumber + partNumber );

                        if( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_UTAC,  LOCATION_TYPE_PRE_TEST);
                            m_preTestDataMap.put( lotNumber + fabLotNumber + partNumber , inventoryLot );
                        }
                        if( result.getInt("Pre test inv") > 0 )
                        {
                            inventoryLot = m_preTestDataMap.get( lotNumber + fabLotNumber + partNumber );

                            if( inventoryLot.getQtyIn() != null )
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( ( Integer.parseInt( inventoryLot.getQtyIn() ) + result.getInt("Pre test inv") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    inventoryLot.setQtyIn( result.getInt("Pre test inv") + "" );
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn( result.getInt("Pre test inv") +"");
                            }
                        }

                        if (inventoryLot.getQtyIn() != null && !"0".equalsIgnoreCase( inventoryLot.getQtyIn() ) )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setStatus(LOT_STATUS_RUNNING);
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setDateCode( result.getString("Date Code"));
                        }
                    }

                    if( result.getInt("XFER-TEST") > 0 || result.getInt("1FT") > 0 || result.getInt("QC B/O TEST") > 0 || result.getInt("TFOI") > 0 )
                    {
                        if ( partNumber.indexOf("-TUS") != -1 )
                        {
                            partNumber = partNumber.substring(0, partNumber.indexOf("-TUS") );
                        }
                        log("In FT" + lotNumber );
                        testLot = m_testDataMap.get( lotNumber + fabLotNumber + partNumber);

                        if (testLot == null)
                        {
                            testLot = new TestLot( VENDOR_NAME_UTAC);
                            m_testDataMap.put( lotNumber + fabLotNumber + partNumber, testLot);
                        }

                        if (result.getInt("XFER-TEST") > 0 )
                        {
                            testLot = m_testDataMap.get( lotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("XFER-TEST") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("XFER-TEST")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("XFER-TEST")+"");
                            }

                            if (testLot.getStage() != null && !"XFER-TEST".equalsIgnoreCase( testLot.getStage() ))
                            {
                                testLot.setStage(testLot.getStage() + ", XFER-TEST");
                            }
                            else
                            {
                                testLot.setStage("XFER-TEST");
                            }
                        }
                        if (result.getInt("1FT") > 0 )
                        {
                            testLot = m_testDataMap.get( lotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("1FT") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("1FT")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("1FT")+"");
                            }

                            if (testLot.getStage() != null && !"FT1".equalsIgnoreCase( testLot.getStage() ))
                            {
                                testLot.setStage(testLot.getStage() + ", FT1");
                            }
                            else
                            {
                                testLot.setStage("FT1");
                            }
                        }
                        if (result.getInt("QC B/O TEST") > 0 )
                        {
                            testLot = m_testDataMap.get( lotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt(testLot.getQtyIn()) + result.getInt("QC B/O TEST") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("QC B/O TEST")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("QC B/O TEST")+"");
                            }

                            if (testLot.getStage() != null && !"QC B/O TEST".equalsIgnoreCase( testLot.getStage() ) )
                            {
                                testLot.setStage(testLot.getStage() + ", QC B/O TEST");
                            }
                            else
                            {
                                testLot.setStage("QC B/O TEST");
                            }
                        }
                        if (result.getInt("TFOI") > 0 )
                        {
                            testLot = m_testDataMap.get( lotNumber + fabLotNumber + partNumber);

                            if (testLot.getQtyIn() != null)
                            {
                                try
                                {
                                    testLot.setQtyIn( (Integer.parseInt(testLot.getQtyIn()) + result.getInt("TFOI") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    testLot.setQtyIn(result.getInt("TFOI")+"");
                                }
                            }
                            else
                            {
                                testLot.setQtyIn(result.getInt("TFOI")+"");
                            }

                            if (testLot.getStage() != null && !"TFOI".equalsIgnoreCase( testLot.getStage() ))
                            {
                                testLot.setStage(testLot.getStage() + ", TFOI");
                            }
                            else
                            {
                                testLot.setStage("TFOI");
                            }
                        }

                        if ( testLot.getQtyIn() != null && !"0".equalsIgnoreCase( testLot.getQtyIn() ) )
                        {
                            testLot.setLotNumber( lotNumber );
                            testLot.setPartNumber( partNumber );
                            if ( result.getString("Hold Status") != null && "HOLD".equalsIgnoreCase( result.getString("Hold Status") ) )
                            {
                                testLot.setStatus( LOT_STATUS_HOLD );
                                testLot.setHoldReason( result.getString("Hold Reason") );
                            }
                            else
                            {
                                testLot.setStatus( LOT_STATUS_RUNNING );
                            }
                            testLot.setFabLotNumber( fabLotNumber );
                            testLot.setPsType( LOCATION_TYPE_FT );
                            testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if (result.getString("Start Date") != null )
                            {
                                testLot.setDateIn( result.getString("Start Date")  );
                            }
                            
                            if (result.getString("Plan Comp Date") != null )
                            {
                                testLot.setExpecetedDate( result.getString("Plan Comp Date") );
                            }
                            testLot.setDateCode( result.getString("Date Code"));
                        }
                    }

                    if (result.getInt("TNR") > 0 || result.getInt("TPACK") > 0  || result.getInt("QC TPACK") > 0)
                    {
                        if ( partNumber.indexOf("-TUS") != -1 )
                        {
                            partNumber = partNumber.substring(0, partNumber.indexOf("-TUS") );
                        }
                        inventoryLot = m_trDataMap.get( partNumber + lotNumber + fabLotNumber );

                        if( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_UTAC,  LOCATION_TYPE_TAPE_REEL);
                            m_trDataMap.put(  partNumber + lotNumber + fabLotNumber , inventoryLot );
                        }
                        if (result.getInt("TNR") > 0 )
                        {
                            inventoryLot = m_trDataMap.get( partNumber + lotNumber + fabLotNumber);

                            if (inventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( ( Integer.parseInt(inventoryLot.getQtyIn()) + result.getInt("TNR") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    inventoryLot.setQtyIn(result.getInt("TNR")+"");
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn(result.getInt("TNR")+"");
                            }

                            if (inventoryLot.getStage() != null && !"TNR".equalsIgnoreCase( inventoryLot.getStage() ))
                            {
                                inventoryLot.setStage(inventoryLot.getStage() + ", TNR");
                            }
                            else
                            {
                                inventoryLot.setStage("TNR");
                            }
                        }
                        if (result.getInt("TPACK") > 0 )
                        {
                            inventoryLot = m_trDataMap.get( partNumber + lotNumber + fabLotNumber);

                            if (inventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( ( Integer.parseInt(inventoryLot.getQtyIn()) + result.getInt("TPACK") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    inventoryLot.setQtyIn(result.getInt("TPACK")+"");
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn(result.getInt("TPACK")+"");
                            }

                            if (inventoryLot.getStage() != null && !"TPACK".equalsIgnoreCase( inventoryLot.getStage() ) )
                            {
                                inventoryLot.setStage(inventoryLot.getStage() + ", TPACK");
                            }
                            else
                            {
                                inventoryLot.setStage("TPACK");
                            }
                        }
                        if (result.getInt("QC TPACK") > 0 )
                        {
                            inventoryLot = m_trDataMap.get( partNumber + lotNumber + fabLotNumber);

                            if (inventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( (Integer.parseInt(inventoryLot.getQtyIn()) + result.getInt("QC TPACK") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    inventoryLot.setQtyIn(result.getInt("QC TPACK")+"");
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn(result.getInt("QC TPACK")+"");
                            }

                            if (inventoryLot.getStage() != null && !"QC TPACK".equalsIgnoreCase(inventoryLot.getStage() ))
                            {
                                inventoryLot.setStage(inventoryLot.getStage() + ", QC TPACK");
                            }
                            else
                            {
                                inventoryLot.setStage("QC TPACK");
                            }
                        }
                        if ( inventoryLot.getQtyIn() != null && !"0".equalsIgnoreCase(inventoryLot.getQtyIn() ) )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            if ( result.getString("Hold Status") != null && "HOLD".equalsIgnoreCase( result.getString("Hold Status") ) )
                            {
                                inventoryLot.setStatus( LOT_STATUS_HOLD );
                                inventoryLot.setHoldReason( result.getString("Hold Reason") );
                            }
                            else
                            {
                                inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            }
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setDateCode( result.getString("Date Code") );
                        }

                    }
                    //FG
                    if ( result.getInt("FGS") > 0  || result.getInt("SFG") > 0)
                    {
                        //log( "In FG " + lotNumber );
                        if ( partNumber.indexOf("-TUS") != -1 )
                        {
                            partNumber = partNumber.substring(0, partNumber.indexOf("-TUS") );
                        }

                        inventoryLot = (InventoryLot) m_fgDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (inventoryLot == null)
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_UTAC, LOCATION_TYPE_FG );
                            m_fgDataMap.put( partNumber + lotNumber + fabLotNumber, inventoryLot);
                        }
                        if (result.getInt("FGS") > 0 )
                        {
                            inventoryLot = m_fgDataMap.get( partNumber + lotNumber + fabLotNumber);

                            if (inventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( (Integer.parseInt(inventoryLot.getQtyIn()) + result.getInt("FGS") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    inventoryLot.setQtyIn(result.getInt("FGS")+"");
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn(result.getInt("FGS")+"");
                            }

                            if (inventoryLot.getStage() != null && !"FGS".equalsIgnoreCase(inventoryLot.getStage() ))
                            {
                                inventoryLot.setStage(inventoryLot.getStage() + ", FGS");
                            }
                            else
                            {
                                inventoryLot.setStage("FGS");
                            }
                        }
                        if (result.getInt("SFG") > 0 )
                        {
                            inventoryLot = m_fgDataMap.get( partNumber + lotNumber + fabLotNumber);

                            if (inventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    inventoryLot.setQtyIn( (Integer.parseInt(inventoryLot.getQtyIn()) + result.getInt("SFG") ) +"" );
                                }
                                catch (Exception e)
                                {
                                    inventoryLot.setQtyIn(result.getInt("SFG")+"");
                                }
                            }
                            else
                            {
                                inventoryLot.setQtyIn(result.getInt("SFG")+"");
                            }

                            if (inventoryLot.getStage() != null && !"SFG".equalsIgnoreCase(inventoryLot.getStage() ))
                            {
                                inventoryLot.setStage(inventoryLot.getStage() + ", SFG");
                            }
                            else
                            {
                                inventoryLot.setStage("SFG");
                            }
                        }

                        if ( inventoryLot.getQtyIn() != null && !"0".equalsIgnoreCase(inventoryLot.getQtyIn() ) )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            if ( result.getString("Hold Status") != null && "HOLD".equalsIgnoreCase( result.getString("Hold Status") ) )
                            {
                                inventoryLot.setStatus( LOT_STATUS_HOLD );
                                inventoryLot.setHoldReason( result.getString("Hold Reason") );
                            }
                            else
                            {
                                inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            }
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setDateCode( result.getString("Date Code"));
                        }
                    }
                }
            }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            System.out.println("!!ERROR !!ERROR Exception while parsting the UTAC Test WIP sheet... = " + e);
            throw e;
        }
    }

   private static void log( String message )
    {

        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}